package br.com.felinelayer.sample;

import br.com.felinelayer.common.Message;
import br.com.felinelayer.common.Protocol;
import br.com.felinelayer.server.application.*;
import java.text.DecimalFormat;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *
 */
public class Sample extends Application {

    private Frame f;
    private Button btGravar;
    private Button btClose;
    private Edit edEnter;
    private Edit edExit;
    private Edit edUnitario;
    private Edit edQuant;
    private Edit edTotal;

    public Sample() {
        setApplicationName("Sample");
    }

    @Override
    protected void doStart() {
        stopAuto = false;
        // Pergunta nome do usuário
        String nome = showInput("Digite seu nome:");
        showMessage("Bem vindo " + nome + "!");
        // 
        f = addFrame().setWidth(560).setHeight(380);
        // 
        f.addLabel().setCol(1).setText("Cliente");
        f.addEdit().setCol(4).setText(nome).setWidth(100);
        // 
        f.addLabel().setCol(10).setText("Enter id");
        f.addLabel().setCol(15).setText("Exit id");
        edEnter = f.addEdit().setCol(13);
        edExit = f.addEdit().setCol(17);
        f.addRow();
        f.addLabel().setCol(1).setText("Código");
        f.addLabel().setCol(6).setText("Descrição");
        f.addLabel().setCol(11).setText("Valor");
        // 
        f.addRow();
        f.addEdit().setCol(1).setText("000001").setWidth(80);
        f.addEdit().setCol(6).setText("MacBook Air").setWidth(100);
        edUnitario = f.addEdit().setCol(11).setText("2.999,99").setWidth(80);

        f.addRow();
        f.addLabel().setCol(6).setText("Quantidade");
        f.addLabel().setCol(11).setText("Total");
        f.addRow();
        edQuant = f.addEdit().setCol(6).setText("2").setWidth(100);
        edTotal = f.addEdit().setCol(11).setText("").setWidth(80);
        //
        f.addRow();
        btGravar = f.addButton().setCol(1).setText("Gravar").setWidth(80);
        f.addRow();
        btClose = f.addButton().setCol(1).setText("Fechar").setWidth(80);
        //
        edQuant.addEventListener(Protocol.EVENT_FOCUS_EXIT, calculateOnExit);
        edUnitario.addEventListener(Protocol.EVENT_FOCUS_EXIT, calculateOnExit);
        edTotal.addEventListener(Protocol.EVENT_FOCUS_EXIT, calculateOnExit);
        //
        f.addRow();
        Grid g = f.addGrid().setCol(1).setWidth(500).setHeight(100);
        g.setModel(getTableModel());
        // 
        f.show();
    }

    private TableModel getTableModel() {
        return new AbstractTableModel() {
            @Override
            public int getRowCount() {
                return 1000;
            }

            @Override
            public int getColumnCount() {
                return 10;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                return rowIndex + " " + columnIndex;
            }
        };
    }

    private void secondFrame() {
        final Frame f2 = addFrame().setLeft(50).setTop(50).setWidth(200).setHeight(200);
        f2.addEdit().setCol(1);
        f2.addRow();
        f2.addEdit().setCol(1);
        f2.addRow();
        Button b = f2.addButton().setCol(1).setText("fechar");
        b.addEventListener(Protocol.EVENT_CLICK, new IEventListener() {
            @Override
            public void onEvent(Component sender, String eventName, Message message) {
                f2.close();
            }
        });
        f2.show();

    }
    private IEventListener calculateOnExit =
            new IEventListener() {
                @Override
                public void onEvent(Component sender, String eventName, Message message) {
                    calcular();
                }
            };

    @Override
    protected void doEventReceived(Component sender, String event) {
        // Se for evento de click
        if (event.equals(Protocol.EVENT_CLICK)) {
            // isso funcionou, blz!
            if (sender.equals(btClose)) {
                sender.destroy();
            }
            //f.close();
            // 
            if (sender.equals(btGravar)) //calcular();
            {
                secondFrame();
            }
        }
        // 
        if (event.equals(Protocol.EVENT_FOCUS_ENTER)) {
            edEnter.setText(sender.getId() + "");
        }
        // 
        if (event.equals(Protocol.EVENT_FOCUS_EXIT)) {
            edExit.setText(sender.getId() + "");
        }
        // Se for evento antes de fechar 
        if (event.equals(Protocol.EVENT_BEFORE_CLOSE)) {
            showMessage("EVENT_BEFORE_CLOSE sender.getText:" + sender.getText());
        }
    }

    private void calcular() {
        String sq = edQuant.getText();
        sq = sq.replace(".", "");
        sq = sq.replace(",", ".");
        //
        float q = Float.parseFloat(sq);
        String su = edUnitario.getText();
        su = su.replace(".", "");
        su = su.replace(",", ".");
        //
        float u = Float.parseFloat(su);
        float t = q * u;
        edTotal.setText(new DecimalFormat("###,###,##0.00").format(t));
    }

    // TODO: NÃO USAR MAIS MÉTODO ABAIXO
    @Override
    protected void doMessageReceived(Message message) {
        String eventName = "";
        if (message.isPropertyExists(Protocol.EVENT_NAME)) {
            eventName = message.getValueByName(Protocol.EVENT_NAME);
        }
        // 
        if (eventName.equals(Protocol.EVENT_BEFORE_CLOSE)) {
            String resposta = showInput("Pode fechar? (\"sim\")");
            if (resposta.equalsIgnoreCase("sim")) {
                message.setValueByName(Protocol.EVENT_BEFORE_CLOSE_CANCLOSE, Protocol.YES);
            } else {
                message.setValueByName(Protocol.EVENT_BEFORE_CLOSE_CANCLOSE, Protocol.NO);
            }
        }
    }
}
